//
//  SKUDetailTbv_V4.m
//  ZITHER
//
//  Created by weixin on 2018/11/9.
//  Copyright © 2018 weixn. All rights reserved.
//

#import "SKUDetailTbv_V4.h"
#import "SKUDetailTbv_V4_Head_Title_Cell.h"
#import "SKUDetailTbv_V4_Price_Chart_Cell.h"
#import "SKUDetailTbv_V4_Buy_Cell.h"
#import "SKUDetailDefultCell_V4.h"
#import "SKUDetailReceiveVC.h"
#import "STViewController.h"
#import "SKUDetailTbv_V4_enquiryPrice_Cell.h"


/** 第三方库新增 */
#import "STRefreshHeader.h"
#import "UIView+STFrame.h"
#import "SwipeTableView.h"


@interface SKUDetailTbv_V4()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,assign)NSInteger itemIndex;
@property (nonatomic,assign)NSInteger numberOfRows;

@property (nonatomic,assign)BOOL state_mj;//判断mj状态
@property (nonatomic,assign)NSInteger index;//tbv 数量

@end

@implementation SKUDetailTbv_V4

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

-(void)refreshData{
    [[serviceDefualt loadskuDetail:self.goodId reponseObjctClass:[skuDetailModel class]]subscribeNext:^(ResponseData* x) {
        if (x.success) {
            self.model  = (skuDetailModel*)x.responseData;
            STViewController *vc = (STViewController*)[self getViewController:[STViewController class]];
            vc.model = self.model;
        }else{
           [Tools showTips:x.errorInfo];
        }
    }];
}

-(void)setGoodId:(NSString *)goodId{
    _goodId = goodId;
    [self refreshData];
}

-(void)setModel:(skuDetailModel *)model{
    _model = model;
    
//    STViewController *vc = (STViewController*)[self getViewController:[STViewController class]];
//    
//    vc.model = model;
    
    [self reloadData];
    if ([model.myEnquiryFlag isEqualToString:@"1"]) {
        self.mj_header.ignoredScrollViewContentInsetTop = kScreenW/350.0*300+40;
    }else{
        self.mj_header.ignoredScrollViewContentInsetTop = kScreenW/350.0*300;
    }

}

- (void)refreshWithData:(id)numberOfRows atIndex:(NSInteger)index {
    _numberOfRows = [numberOfRows integerValue];
    _itemIndex = index;
//    [Tools showTips:@"走了 refreshWithData:(id)numberOfRows 这方法"];
    [self reloadData];
}

#pragma mark -- tabdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger index= 2 ;
    
    if (![self.model.myEnquiryFlag isEqualToString:@"1"]) {
        index++;
    }
    
    if(self.model.attributes.count>0){
        index++;
    }
    
    if (![self.model.openSourceCount isEqualToString:@"0"]) {
        index++;
    }
    self.index = index;
    return index;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
return;    }else if (indexPath.row == 1){
        //       int priceInt = [self.model.price intValue];
        //        if (priceInt == 0) {
        //
        //        }
return;
    }else if(self.model.attributes.count>0 && indexPath.row == 2){
        
return;
    }else{
        /**
         当属性为0时row ==2时
         当属性为1时 row == 3
         */
        
        
        
        if(self.model.attributes.count==0 && indexPath.row ==2){
            if (![self.model.myEnquiryFlag isEqualToString:@"1"]) {
return;            }
        }
        
        if (indexPath.row == 3 && self.model.attributes.count>0) {
            if (![self.model.myEnquiryFlag isEqualToString:@"1"]) {
return;            }
        }
        
        SKUDetailReceiveVC *vc = [[SKUDetailReceiveVC alloc]init];
        vc.indexType = @"2";
        vc.str = @"提醒商家报价";
        vc.leb.text = @"公开货源";
        vc.goodsid = self.model.goodsId;
        vc.IsEnquiry = ![self.model.myEnquiryFlag isEqualToString:@"1"];
        [self.getNav pushViewController:vc animated:YES];
        
        
        //        if (self.index == 4) {
        //
        //        }
        //
        //        if (![self.model.myEnquiryFlag isEqualToString:@"1"] && indexPath.row == 2 ) {
        //            return [self getSKUDetailTbv_V4_enquiryPrice_Cell:indexPath];
        //        }
        //        if (![self.model.myEnquiryFlag isEqualToString:@"1"] && indexPath.row == 3 ) {
        //            return [self getSKUDetailTbv_V4_enquiryPrice_Cell:indexPath];
        //        }
        //        return [self getSKUDetailDefultCell_V4:indexPath];
        
    }
    
    
    
//    if (indexPath.row == 0) {
//        return ;
//    }else if (indexPath.row == 1){
//        return ;
//    }else if(self.model.attributes.count>0 && indexPath.row == 2){
//       return ;
//    }
//
//    if(self.model.attributes.count==0 && indexPath.row ==2){
//        return ;
//    }
//
//    if (indexPath.row == 3 && self.model.attributes.count>0) {
//        return;
//    }
    
    

    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return  [self getSKUDetailTbv_V4_Head_Title_Cell:indexPath];
    }else if (indexPath.row == 1){
//       int priceInt = [self.model.price intValue];
//        if (priceInt == 0) {
//            
//        }
        return [self getSKUDetailTbv_V4_Price_Chart_Cell:indexPath];

    }else if(self.model.attributes.count>0 && indexPath.row == 2){
        
        return [self getSKUDetailTbv_V4_Buy_Cell:indexPath];
        
    }else{
        /**
         当属性为0时row ==2时
         当属性为1时 row == 3
         */
        
       
        
        if(self.model.attributes.count==0 && indexPath.row ==2){
            if (![self.model.myEnquiryFlag isEqualToString:@"1"]) {
                return [self getSKUDetailTbv_V4_enquiryPrice_Cell:indexPath];
            }
        }
        
        if (indexPath.row == 3 && self.model.attributes.count>0) {
            if (![self.model.myEnquiryFlag isEqualToString:@"1"]) {
                return [self getSKUDetailTbv_V4_enquiryPrice_Cell:indexPath];
            }
        }
        
        return [self getSKUDetailDefultCell_V4:indexPath];

        
        
//        if (self.index == 4) {
//
//        }
//
//        if (![self.model.myEnquiryFlag isEqualToString:@"1"] && indexPath.row == 2 ) {
//            return [self getSKUDetailTbv_V4_enquiryPrice_Cell:indexPath];
//        }
//        if (![self.model.myEnquiryFlag isEqualToString:@"1"] && indexPath.row == 3 ) {
//            return [self getSKUDetailTbv_V4_enquiryPrice_Cell:indexPath];
//        }
//        return [self getSKUDetailDefultCell_V4:indexPath];

    }
}


-(UITableViewCell*)getSKUDetailTbv_V4_enquiryPrice_Cell:(NSIndexPath*)index{
    
    static NSString *sectionCell = @"SKUDetailTbv_V4_enquiryPrice_Cell";
    SKUDetailTbv_V4_enquiryPrice_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKUDetailTbv_V4_enquiryPrice_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = self.model;
    @weakify(self);
    [cell setOpenQuestion:^{
        @strongify(self);
        
        if (self.OpenQuestion) {
            self.OpenQuestion();
        }
        //        [Tools showTips:@"开始询价"];
        //        [self reloaddata];
        //        //            NSLog(@"开始询价");
        //        self.Questionstate = NO;
    }];

    return cell;
}

-(UITableViewCell*)getSKUDetailTbv_V4_Buy_Cell:(NSIndexPath*)index{

    static NSString *sectionCell = @"SKUDetailTbv_V4_Buy_Cell";
    SKUDetailTbv_V4_Buy_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKUDetailTbv_V4_Buy_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = self.model;
    
    [cell setBlcok:^{
        [self beginUpdates];
//        [self reloadData];
        [self endUpdates];
        
    }];
    return cell;

}

-(UITableViewCell*)getSKUDetailTbv_V4_Head_Title_Cell:(NSIndexPath*)index{

    static NSString *sectionCell = @"SKUDetailTbv_V4_Head_Title_Cell";
    SKUDetailTbv_V4_Head_Title_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKUDetailTbv_V4_Head_Title_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = self.model;
    return cell;
}

-(UITableViewCell*)getSKUDetailTbv_V4_Price_Chart_Cell:(NSIndexPath*)index{

    static NSString *sectionCell = @"SKUDetailTbv_V4_Price_Chart_Cell";
    SKUDetailTbv_V4_Price_Chart_Cell *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKUDetailTbv_V4_Price_Chart_Cell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = self.model;
    [cell setBlcok:^{

        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];

        [self reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];

    }];
    
    return cell;
}

-(UITableViewCell*)getSKUDetailDefultCell_V4:(NSIndexPath*)index{

    static NSString *sectionCell = @"SKUDetailDefultCell_V4";
    SKUDetailDefultCell_V4 *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[SKUDetailDefultCell_V4 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = self.model;
    [cell setIndexCell:2];
    return cell;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {


}
@end
